package article_service

import (
	"hytx_media/models"
	"time"
)

type Article struct {
	ID                int
	Author            int
	Title             string
	LabelId           string
	Content           string
	Cover             string
	IsAd              bool
	ArticleCategoryId int
	Sort              int
	Creator           int
	Pageviews         int
	ActualPageviews   int
	CommentNum        int
	LastCommentAt     time.Time
	Status            int
	OperateStatus     int
	ShareNum          int
	RecommendNum      int
	CollectNum        int
	CreatedAt         time.Time
	UpdatedAt         time.Time
	IsDeleted         bool
	PageNum           int
	PageSize          int
	BeginTime         string
	EndTime           string
	Thumb			  int
}

func (a *Article) Add() error {

	article := map[string]interface{}{
		"title":               a.Title,
		"label_id":            a.LabelId,
		"content":             a.Content,
		"cover_path":          a.Cover,
		"is_ad":               a.IsAd,
		"article_category_id": a.ArticleCategoryId,
		"sort":                a.Sort,
		"author":              a.Author,
		"status":              a.Status,
	}

	if err := models.AddArticle(article); err != nil {
		return err
	}
	return nil
}

// 获取单个文章
func (a *Article) Get() (*models.Article, error) {

	article, err := models.GetArticle(a.ID)
	if err != nil {
		return nil, err
	}

	return article, nil

}

// 分页插叙 文章列表
func (a *Article) GetAll() ([]*models.Article, error) {
	var (
		articles []*models.Article
	)
	var title string
	if a.Title != "" {
		title = "title like" + " " + "'" + "%" + a.Title + "%" + "'"
	}
	var timeWhere string
	if a.BeginTime != "" && a.EndTime != "" {
		timeWhere = "`created_at` >=" + " " + "'" + a.BeginTime + "'" + " " + "and `created_at`<" + "'" + a.EndTime + "'"
	}
	articles, err := models.GetArticles(title, timeWhere, a.PageNum, a.PageSize, a.getMaps())
	if err != nil {
		return nil, err
	}
	return articles, nil

}

// 统计查询文章条目数
func (a *Article) Count() (int, error) {
	var title string
	if a.Title != "" {
		title = "title like" + " " + "'" + "%" + a.Title + "%" + "'"
	}
	var timeWhere string
	if a.BeginTime != "" && a.EndTime != "" {
		timeWhere = "`created_at` >" + " " + "'" + a.BeginTime + "'" + " " + "and `created_at`<=" + "'" + a.EndTime + "'"
	}
	return models.GetArticleTotal(title, timeWhere, a.getMaps())
}

//
func (a *Article) ExistByID() (bool, error) {
	return models.ExistArticleByID(a.ID)
}

// 修改

func (a *Article) Edit() error {
	return models.EditArticle(a.ID, map[string]interface{}{
		"title":               a.Title,
		"label_id":            a.LabelId,
		"content":             a.Content,
		"cover":          a.Cover,
		"article_category_id": a.ArticleCategoryId,
		"is_ad":               a.IsAd,
		"status":              a.Status,
		"operate_status":      a.OperateStatus,
	})
}

// 文章撤回
func (a *Article) Revoke() error {
	return models.RevokeArticle(a.ID, map[string]interface{}{
		"status":        a.Status,
		"operate_status": a.OperateStatus,
	})
}

// 查询调价的拼装
func (a *Article) getMaps() map[string]interface{} {
	maps := make(map[string]interface{})
	maps["is_deleted"] = 0
	maps["author"] = a.Author
	if a.Status != -1 {
		maps["status"] = a.Status
	}

	return maps
}

//删除
func (a *Article) Deleted() error {
	return models.DeletedArticle(a.ID)
}

// 统计操作次数
func (a *Article) SumOperaNum(opera string) (int, error) {
	return models.SumOpera(a.ID, opera)
}
